﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using FastFood.Business.Entities;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace FastFood.DataAccess
{
    public class FormularioDalc
    {
        Database _menuDB;
        private FormularioDalc() 
        {
            _menuDB = DatabaseFactory.CreateDatabase();
        }
        private static readonly FormularioDalc _Instancia = new FormularioDalc();
        public static FormularioDalc Instancia
        {
            get { return FormularioDalc._Instancia; }
        }

        public Formularios ObtenerFormularioPorID(byte id)
        {
            Formularios f = new Formularios();
            DbCommand dbCommand = _menuDB.GetStoredProcCommand("proc_FormulariosLoadByPrimaryKey");
            _menuDB.AddInParameter(dbCommand, "formularioID", DbType.Byte, id);
            using (IDataReader lector = _menuDB.ExecuteReader(dbCommand))
            {
                while (lector.Read())
                {
                    f = new Formularios();
                    f.formularioID = (byte)lector["formularioID"];
                    f.menuID = (byte)lector["menuID"];
                    f.nombre = (string)lector["nombre"];
                    f.nombreClase = (string)lector["nombreClase"];
                    f.descripcion = lector["descripcion"] is DBNull ? string.Empty : (string)lector["descripcion"];
                    f.estado = (bool)lector["estado"];
                    f.permisosAsignables = (byte)lector["permisosAsignables"];
                }
            }
            return f;
        }

        public List<Formularios> ObtenerFormulariosPorMenuID(byte id)
        {
            List<Formularios> lista = new List<Formularios>();
            DbCommand dbCommand = _menuDB.GetStoredProcCommand("proc_FormulariosLoadByMenuID");
            _menuDB.AddInParameter(dbCommand, "menuID", DbType.Byte, id);
            using (IDataReader lector = _menuDB.ExecuteReader(dbCommand))
            {
                Formularios f;
                while (lector.Read())
                {
                    f = new Formularios();
                    f.formularioID = (byte)lector["formularioID"];
                    f.menuID = (byte)lector["menuID"];
                    f.nombre = (string)lector["nombre"];
                    f.nombreClase = (string)lector["nombreClase"];
                    f.descripcion = lector["descripcion"] is DBNull ? string.Empty : (string)lector["descripcion"];
                    f.estado = (bool)lector["estado"];
                    f.permisosAsignables = (byte)lector["permisosAsignables"];
                    lista.Add(f);
                }
            }
            return lista;
        }

        public List<Formularios> ObtenerFormulariosPorUsuarioID(short id)
        {
            List<Formularios> lista = new List<Formularios>();
            DbCommand dbCommand = _menuDB.GetStoredProcCommand("proc_FormulariosByUsuario");
            _menuDB.AddInParameter(dbCommand, "usuarioID", DbType.Int16, id);
            using (IDataReader lector = _menuDB.ExecuteReader(dbCommand))
            {
                Formularios f;
                while (lector.Read())
                {
                    f = new Formularios();
                    f.formularioID = lector.GetByte(0);
                    f.menuID = lector.GetByte(1);
                    f.nombre = lector.GetString(2);
                    f.nombreClase = lector.GetString(3);
                    f.descripcion = lector.IsDBNull(4) ? string.Empty : lector.GetString(4);
                    f.estado = lector.GetBoolean(5);
                    f.permisosAsignables = lector.GetByte(6);
                    lista.Add(f);
                }
            }
            return lista;
        }
    }
}
